package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;

/**
 * ---标准合同
 */
public class StandardCompactActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ImageView iv_choose_img;
    private boolean isTrue;

    private final static int CODE_COMPACT = 946;

    private String url;
    private String msg;

    private WebView webview;
    private String data = "0";

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
//                    activity.closeDialog();
                    break;

                default:
                    break;
            }
        }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard_compact);
        initTitle();

        AlertDialog.Builder builder = new AlertDialog.Builder(StandardCompactActivity.this);
        final View view = View.inflate(StandardCompactActivity.this, R.layout.dialog_standard, null);
        builder.setView(view);
        final AlertDialog show = builder.show();
        Window dialogWindow = show.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);
        TextView queding1 = (TextView) view.findViewById(R.id.queding1);
        queding1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show.dismiss();
                Intent i = new Intent();
                i.putExtra("pact", "0");
                setResult(RESULT_OK, i);
                finish();
            }
        });
        show.setCancelable(false);
        initData();
        initView();
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("自定义合同");
        //findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private void initView() {


    }

    private static final String TAG = "StandardCompactActivity";

    private void initData() {
        url = getIntent().getStringExtra("url");

        Log.i(TAG, "initData: " + url);
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //选择
            case R.id.rl_choose:
                isTrue = !isTrue;
                if (isTrue) {
                    iv_choose_img.setImageResource(R.mipmap.ic_choose_nor);
                    isTrue = true;
                    data = "0";
                } else {
                    iv_choose_img.setImageResource(R.mipmap.ic_choose_li);
                    data = "1";
                }
                break;

            //提交
            case R.id.tv_submit:
                if ("0".equals(data)) {
                    MyToast("请选择同意合同内容,如不同意请选择自定义合同");
                } else {
                    intent = new Intent(this, ProjectOrderActivity.class);
                    intent.putExtra("pact", "1");
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;

            //选择自定义合同
            case R.id.tv_standard:
                intent = new Intent(this, UserDefinedCompactActivity.class);
                intent.putExtra("msg", msg);
                startActivityForResult(intent, CODE_COMPACT);
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CODE_COMPACT:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        String pact = data.getStringExtra("pact");
                        if ("0".equals(pact)) {
                            Intent intent = new Intent(this, ProjectOrderActivity.class);
                            intent.putExtra("pact", "0");
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
}
